'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports ua
Imports System.Net

Module Module1

    Public bolSilent As Boolean = False

    'configuration data
    Private NewConfigurationData As New ConfigurationData

    Sub Main()

        Dim NewInet As New System.Net.WebClient
        Dim RetVal As DialogResult

        Try
            'already running, exit
            Dim myProcesses As Process() = Process.GetProcessesByName("UAUpdate")
            If myProcesses.Length > 1 Then End

            'check if we are to run silent
            Dim CommandLineArgs As String() = System.Environment.GetCommandLineArgs()
            If CommandLineArgs.Length = 2 Then
                If CommandLineArgs(1) = "-silent" Then bolSilent = True
            End If

            'load config to set proxy
            If LoadConfiguration(NewConfigurationData) = False Then
                MessageBox.Show("Update Failed! Couldn't load Configuration file.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
                End
            End If

            'get version number
            Dim VersionNumber As String = System.Text.Encoding.ASCII.GetString(NewInet.DownloadData("http://usageagent.sourceforge.net/version.txt"))

            'notify user
            If Val(Replace(VersionNumber, ".", "")) > Val(Replace(Application.ProductVersion, ".", "")) Then
                'get whats new
                Dim WhatsNew As String = System.Text.Encoding.ASCII.GetString(NewInet.DownloadData("http://usageagent.sourceforge.net/new.txt"))

                RetVal = MessageBox.Show("A new version of Usage Agent is available." & vbCrLf & vbCrLf & WhatsNew & vbCrLf & vbCrLf & "Open the Usage Agent website?", "Usage Agent", MessageBoxButtons.YesNo, MessageBoxIcon.Information)

                If RetVal = Windows.Forms.DialogResult.Yes Then
                    System.Diagnostics.Process.Start("http://usageagent.sourceforge.net/download.htm")
                End If
            Else
                If bolSilent = False Then MessageBox.Show("No updates are available.", "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

            NewInet.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            'if connection is still open, then close it
            If NewInet IsNot Nothing Then NewInet.Dispose()
        End Try
    End Sub

End Module



